/*
 * arch/ckcore/ck6408evb/irq.c ---IRQ vector handles
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2006 Hangzhou C-SKY Microsystems co.,ltd.
 * Copyright (C) 2009 by Hu Junshan <junshan_hu@c-sky.com> 
 */
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <asm/irq.h>
#include <asm/cacheflush.h>
#include <mach/gx6002_irq.h>

/*
 *  Mask the interrupt and the irp number is irqno.
 */
static void
gx6002_irq_mask(unsigned int irqno)
{
	*(volatile unsigned int *)(BASE_INTC+0x08) |= (0x1 << irqno);
}

/*
 *  Unmask the interrupt and the irp number is irqno.
 */
static void
gx6002_irq_unmask(unsigned int irqno)
{
	*(volatile unsigned int *)(BASE_INTC+0x08) &= ~(0x1 << irqno);
}

struct irq_chip gx6002_irq_chip = {
        .name           = "gx6002",
        .mask           = gx6002_irq_mask,
        .unmask         = gx6002_irq_unmask,
};

void gx6002_disable_extra_intc(void)
{
	//close extra interrupt
	*(volatile unsigned int *)(BASE_INTC+0x14) &= (0x1<<1);	
}


/*
 *  Initial the interrupt controller of c-sky.
 */
void __init gx6002_init_IRQ(void)
{
	volatile unsigned int *icrp;
	int i;
	
	icrp = (volatile unsigned int *) (BASE_INTC);

	gx6002_disable_extra_intc();
	 //set timer interrupt num
	*(volatile unsigned int *)(BASE_INTC+0x40) |= (0x1<<5); 
	// mask timer interrupt 
	//*(volatile unsigned int *)(BASE_INTC+0x8) |= (0x1<<1);  
	  //enable timer interrupt
	*(volatile unsigned int *)(BASE_INTC+0x14) |= (0x1<<1);

	//set uart interrupt num
	*(volatile unsigned int *)(BASE_INTC+0x40) |= (0x0<<0);
	// mask uart interrupt 
	//*(volatile unsigned int *)(BASE_INTC+0x8) |= (0x1<<0);  
	//enable uart interrupt 
	*(volatile unsigned int *)(BASE_INTC+0x14) |= (0x1<<0); 
	//set MAC interrupt num
	*(volatile unsigned int *)(BASE_INTC+0x40) |= (0xc<<10);
	// mask MAC interrupt 
	*(volatile unsigned int *)(BASE_INTC+0x8) |= (0x1<<2);  
	//enable MAC interrupt 
	*(volatile unsigned int *)(BASE_INTC+0x14) |= (0x1<<2); 
	
	for (i = 0; i < NR_IRQS; i++)
		set_irq_chip(i, &gx6002_irq_chip);
}

unsigned int gx6002_check_interrupt_num(void)
{
	unsigned int num, i;

	num = *(volatile unsigned int *)(BASE_INTC);
	
	for(i=0;i<32;i++)
	{
		if(num & (0x1<<i))
		{
			return i;
		}
	}
	
	return -1;
}

